﻿#include "coder.h"
#include "ui_coder.h"

Coder::Coder(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Coder)
{
    ui->setupUi(this);

    setAutoClosed(true);
}

Coder::~Coder()
{
    delete ui;
}

void Coder::setAutoClosed(bool flag)
{
    isAutoClosed=flag;

    if(isAutoClosed)
#ifdef QT_NO_DEBUG
        timerId=startTimer(300 * 1000);
#else
        timerId=startTimer(10 * 1000);
#endif
}

void Coder::timerEvent(QTimerEvent *event)
{
    if(timerId == event->timerId())
    {
        if(isAutoClosed)
            this->close();
    }
}

void Coder::on_btn_search_clicked()
{
    QString code=ui->code_in->text().trimmed();
    if(code.isEmpty())
        return;

    QSqlQuery query;
    query.exec(QString("select detail from %1 where label = '%2'")
               .arg(TD_CODE)
               .arg(code));

    if(query.next())
    {
        QString detail=query.value(0).toString();
        ui->lbl_display->setText(detail);
    }
    else
        ui->lbl_display->setText(QStringLiteral("没有找到相应产品！"));
}
